
import 'package:flutter/material.dart';
import 'package:flutterdemo/main.dart';
import 'package:flutterdemo/smartform/container/StackPage.dart';
import 'package:flutterdemo/smartform/utils/log.dart';

enum LiftCycleStatus{
  INIT,
  CREATE,
  RESUME,
  PAUSE,
  DESTROY
}

class Entity{
  String value = "" ;
  int key = 0;
  Entity({
    this.value = "",
    this.key =  0});
}

mixin LiftCycle{
  void onCreate();
  void onResume();
  void onPause();
  void onDestroy();
  Widget onBuildWidget(BuildContext context);
}

/// 生命周期管理类
abstract class  LiftCycleBaseWidget<T extends AbsLiftCycleBaseState> extends StatefulWidget{

  @override
  AbsLiftCycleBaseState createState() => getState();

  AbsLiftCycleBaseState getState();



}

abstract class AbsLiftCycleBaseState<T extends StatefulWidget> extends _LiftCycleBaseState<T>{


  @override
  Widget onBuildWidget(BuildContext context);


}

abstract class _LiftCycleBaseState<T extends StatefulWidget> extends State<T> with RouteAware, LiftCycle, WidgetsBindingObserver{

  LiftCycleStatus liftCycleStatus = LiftCycleStatus.INIT;
  String runType  = "";

  @override
  void initState() {
    super.initState();
    runType = runtimeType.toString();
    Log.logWsq("$runType initState  ${liftCycleStatus}");

    WidgetsBinding.instance.addObserver(this);

    if(liftCycleStatus != LiftCycleStatus.CREATE){

      liftCycleStatus = LiftCycleStatus.CREATE;
      Log.logWsq("$runType initState  ${liftCycleStatus}");
      onCreate();
    }

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      Log.logWsq("$runType addPostFrameCallback");
      if(liftCycleStatus != LiftCycleStatus.RESUME){
        liftCycleStatus = LiftCycleStatus.RESUME;
        onResume();
      }
    });

  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    Log.logWsq("$runType didChangeDependencies");
    routeObserver.subscribe(this, ModalRoute.of(context) as Route);
  }

  @override
  void didUpdateWidget(covariant T oldWidget) {
    super.didUpdateWidget(oldWidget);
    Log.logWsq("$runType didUpdateWidget");
  }

  @override
  Widget build(BuildContext context) {
    if(liftCycleStatus != LiftCycleStatus.RESUME){
      liftCycleStatus = LiftCycleStatus.RESUME;
      onResume();
    }
    Log.logWsq("$runType build");
   return onBuildWidget(context);
  }

  void onClick(){
    Navigator.push(context, MaterialPageRoute(builder: (context){
      return  StackPage() ;
    }));

  }

  @override
  void deactivate() {
    super.deactivate();
    Log.logWsq("$runType deactivate");
    if(liftCycleStatus != LiftCycleStatus.DESTROY){
      liftCycleStatus = LiftCycleStatus.DESTROY;
      onDestroy();
    }
  }

  @override
  void dispose() {
    super.dispose();
    Log.logWsq("$runType dispose");
    routeObserver.unsubscribe(this);

    WidgetsBinding.instance.removeObserver(this);
    if(liftCycleStatus != LiftCycleStatus.DESTROY){
      liftCycleStatus = LiftCycleStatus.DESTROY;
      onDestroy();
    }

  }

  @override
  void didPush() {
    super.didPush();
    Log.logWsq("$runType didPush");
    if(liftCycleStatus != LiftCycleStatus.RESUME){
      liftCycleStatus = LiftCycleStatus.RESUME;
      onResume();
    }
  }

  @override
  void didPop() {
    super.didPop();
    Log.logWsq("$runType didPop");
    if(liftCycleStatus != LiftCycleStatus.DESTROY){
      liftCycleStatus = LiftCycleStatus.DESTROY;
      onDestroy();
    }
  }

  @override
  void didPopNext() {
    super.didPopNext();
    Log.logWsq("$runType didPopNext");
    if(liftCycleStatus != LiftCycleStatus.RESUME){
      liftCycleStatus = LiftCycleStatus.RESUME;
      onResume();
    }
  }

  @override
  void didPushNext() {
    super.didPushNext();
    Log.logWsq("$runType didPushNext");
    if(liftCycleStatus != LiftCycleStatus.PAUSE){
      liftCycleStatus = LiftCycleStatus.PAUSE;
      onPause();
    }
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    /// todo flutter引擎的生命周期
    if (state == AppLifecycleState.resumed) {
      Log.logWsq("$runType app进入前台");
      if(liftCycleStatus != LiftCycleStatus.RESUME){
        liftCycleStatus = LiftCycleStatus.RESUME;
        onResume();
      }
    }
    if (state == AppLifecycleState.inactive) {
      Log.logWsq("$runType app在前台但不响应事件，比如电话，touch id等");
    }
    if (state == AppLifecycleState.paused) {
      Log.logWsq("$runType app进入后台");
      if(liftCycleStatus != LiftCycleStatus.PAUSE){
        liftCycleStatus = LiftCycleStatus.PAUSE;
        onPause();
      }
    }

    if (state == AppLifecycleState.detached) {
      Log.logWsq("$runType 没有宿主视图但是flutter引擎仍然有效");
      if(liftCycleStatus != LiftCycleStatus.DESTROY){
        liftCycleStatus = LiftCycleStatus.DESTROY;
        onDestroy();
      }
    }
  }




}

